package org.jiajie.coffeecodes.concurrent.poison;

import java.util.ArrayList;
import java.util.List;

/**
 * 这里采用同步锁来控制添加"毒丸"和"步骤"是互斥的。
 * @author jay
 */
public class TaskCancelManager {
    private static final String POISON = "poison";
    private List<String> stepNames = new ArrayList<>();

    public synchronized boolean addStep(String stepName){
        // 如果上一个是毒丸
        if (stepNames.size() > 0 && stepNames.get(stepNames.size() - 1).equals(POISON)){
            return false;
        }
        stepNames.add(stepName);
        return true;
    }

    public synchronized void addPoison(){
        stepNames.add(POISON);
    }

    public List<String> getStepNames(){
        return stepNames;
    }
}
